package cn.itrip.common;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class phone {

    private static final String  QUEAR_PATH="https://api.miaodiyun.com/20150822/industrySMS/sendSMS";
    private static final String  ACCOUNT_SID="2a2810bfc500435b8608edb8ec60524d";
    private static final String  AUTH_TOKEN="6bf40eca98c04f75acf6f8ebc9fadbdf";

    /**
     * 根据用户手机发送验证码
     * @param shouji 手机号
     * @return  返回验证码
     */
    public static String getcode(String shouji){
        String  getsuiji=suiji();
        String time=gettime();
        String name=getname(ACCOUNT_SID, AUTH_TOKEN,time);
        String tamp="【博城科技】您的验证码为"+getsuiji+"，请于2分钟内正确输入，如非本人操作，请忽略此短信。";
        OutputStreamWriter out=null;
        BufferedReader bu=null;
        StringBuilder res=new StringBuilder();
        try {
            URL url=new URL(QUEAR_PATH);
            HttpURLConnection connection=(HttpURLConnection)url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoInput(true);//设置是否允许数据写入
            connection.setDoOutput(true);//设置是否语序参数输出
            connection.setConnectTimeout(5000);//设置链接响应时间
            connection.setReadTimeout(1000);//设置参数读取时间
            connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");

            //提交请求
            out=new OutputStreamWriter(connection.getOutputStream(),"UTF-8");
            out.write(get(ACCOUNT_SID,tamp,shouji,time,name,"JSON"));
            out.flush();

            bu=new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
            String temp="";
            while((temp=bu.readLine())!=null){
                res.append(temp);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return getsuiji;

    }
    //字符串拼接
    private static String get(String id,String smscontent,String to,String time,String sig,String res){
        String aaa="accountSid="+id+"&smsContent="+smscontent+" &to="+to+"&timestamp="+time+"&sig="+sig+"&respDataType="+res;
        return aaa;
    }

    //返回时间
    private static String gettime(){

        return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    }

    //md5加密
    private static String getname(String sid,String token,String time){
        StringBuilder re=new StringBuilder();
        String source=sid+token+time;
        try {
            MessageDigest digest= MessageDigest.getInstance("MD5");
            byte[] ny=digest.digest(source.getBytes());
            for (byte b : ny) {
                String hex= Integer.toHexString(b&0xff);
                if(hex.length()==1){
                    re.append("0"+hex);
                }else{
                    re.append(hex);
                }
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return re.toString();
    }
    //随机数
    private static String suiji() {
        String ran = new Random().nextInt(10000) + "";
        if (ran.length() != 4) {
            return suiji();
        } else {
            return ran;
        }
    }

    public static void main(String[] args) {
//        int sum = 0,i=0;
//        for (;;){
//            if(i>10){
//                break;
//            }
//            sum+=i++;
//        }
//        System.out.println(sum);
        for(int i=0;i<=10;i++){
            getcode("17682480559");
        };
    }
}
